package com.gitee.leijin.rbac.security;

import cn.hutool.json.JSONUtil;
import com.gitee.leijin.rbac.util.AjaxUtils;
import com.gitee.leijin.rbac.util.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import org.springframework.security.web.access.ExceptionTranslationFilter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @see ExceptionTranslationFilter 出现AuthenticationException 或者 AccessDeniedException 会回调此方法 commence
 */
@Component
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        Result<String> loginResult = Result.failure("请先登录");
        AjaxUtils.response(response, JSONUtil.toJsonStr(loginResult));
    }
}
